/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.CompanyDAO;
import com.opgea.token.dao.DestinationDAO;
import com.opgea.token.dao.EmployeeDAO;
import com.opgea.token.dao.EmployeeDocumentDAO;
import com.opgea.token.dao.LoginDAO;
import com.opgea.token.domain.constants.ApplicationConstants;
import com.opgea.token.domain.entities.Company;
import com.opgea.token.domain.entities.Destination;
import com.opgea.token.domain.entities.Employee;
import com.opgea.token.domain.entities.EmployeeDocument;
import com.opgea.token.domain.entities.FileInfo;
import com.opgea.token.domain.entities.Login;
import com.opgea.token.domain.model.PrimeFacesFileUploadBean;
import com.opgea.token.domain.qualifiers.EmployeeDocumentType;
import com.opgea.token.service.EmployeeService;
import com.opgea.token.web.dto.EmployeeDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDAO employeeDAO;
    @Autowired
    private CompanyDAO companyDAO;
    @Autowired
    private DestinationDAO destinationDAO;
    @Autowired
    private LoginDAO loginDAO;
    @Autowired
    private EmployeeDocumentDAO documentDAO;

    @Override
    public EmployeeDTO create(EmployeeDTO employeeDTO) {
	Employee employee = new Employee();
	Company company = companyDAO.find(employeeDTO.getCompanyId());
	Destination destination = destinationDAO.find(employeeDTO
		.getDestinationId());

	employeeDTO.unmarshall(employee);
	employee.setCompany(company);
	employee.setDestination(destination);
	employeeDAO.create(employee);

	Login login = new Login();
	login.setLoginId(employeeDTO.getEmailId());
	login.setPassword(ApplicationConstants.DEFAULT_PASSWORD);
	login.setEmployee(employee);
	loginDAO.create(login);

	return employeeDTO;
    }

    @Override
    public EmployeeDTO update(EmployeeDTO employeeDTO) {
	Employee employee = null;
	if (employeeDTO.getId() != null && employeeDTO.getId() > 0) {
	    Destination destination = destinationDAO.find(employeeDTO
		    .getDestinationId());
	    employee = employeeDAO.find(employeeDTO.getId());
	    employeeDTO.unmarshall(employee);
	    employee.setDestination(destination);
	    employeeDAO.update(employee);
	} else {
	    this.create(employeeDTO);
	}

	return employeeDTO;
    }

    @Override
    public List<EmployeeDTO> findAll() {
	List<Employee> empList = employeeDAO.findAll();
	List<EmployeeDTO> empDTOList = new ArrayList<EmployeeDTO>();
	for (Employee employee : empList) {
	    EmployeeDTO empDTO = new EmployeeDTO();
	    empDTO.marshall(employee);
	    empDTOList.add(empDTO);
	}
	return empDTOList;
    }

    @Override
    public List<EmployeeDTO> findAllByCompanyId(Long companyId) {
	List<Employee> empList = employeeDAO.findAllByCompanyId(companyId);
	List<EmployeeDTO> empDTOList = new ArrayList<EmployeeDTO>();
	for (Employee employee : empList) {
	    EmployeeDTO empDTO = new EmployeeDTO();
	    empDTO.marshall(employee);
	    empDTOList.add(empDTO);
	}
	return empDTOList;
    }

    @Override
    public EmployeeDTO find(Long employeeId) {
	Employee employee = employeeDAO.find(employeeId);
	EmployeeDTO dto = new EmployeeDTO();
	dto.marshall(employee);
	return dto;
    }

    @Override
    public EmployeeDTO delete(Long employeeId) {
	Employee employee = employeeDAO.delete(employeeId);
	EmployeeDTO dto = new EmployeeDTO();
	dto.marshall(employee);
	return dto;
    }

    @Override
    public void uploadDocument(Long employeeId,
	    PrimeFacesFileUploadBean pfFileUploadBean) {
	UploadedFile file = pfFileUploadBean.getFile();
	Employee employee = employeeDAO.find(employeeId);
	byte[] content = file.getContents();
	EmployeeDocument document = new EmployeeDocument();
	FileInfo fileInfo = new FileInfo();
	fileInfo.setExtension(file.getFileName().substring(
		file.getFileName().lastIndexOf(".")));
	fileInfo.setName(file.getFileName());
	fileInfo.setSize(file.getSize());

	document.setDocument(content);
	document.setDocumentType(EmployeeDocumentType.PERSONAL_IMAGE.toString());
	document.setFileInfo(fileInfo);
	documentDAO.create(document);
	employee.addDocument(document);
	employeeDAO.update(employee);
    }

}
